/**     
 * @Title: UserVoController.java   
 * @Package me.swagger2.controller   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年3月29日 上午10:57:08   
 * @version V1.0     
 */
package me.swagger2.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.PostConstruct;

import me.swagger2.vo.UserVo;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: UserVoController
 * @Description: TODO
 * @author weiwei
 * @date 2018年3月29日 上午10:57:08
 * 
 */
@RestController
@RequestMapping(value = "/v1/user")
public class UserController {

	// 当作 id 生成器。
	private AtomicLong idCount = new AtomicLong(0);

	private Map<Long, UserVo> userVos = new ConcurrentHashMap<Long, UserVo>();

	@PostConstruct
	public void init() {
		addUserVo("xaoyaoyao1");
		addUserVo("xaoyaoyao2");
		addUserVo("xaoyaoyao3");
	}

	@ApiOperation(value = "创建用户", notes = "根据用户名创建用户")
	@ApiImplicitParam(name = "name", value = "用户名称", required = true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	public UserVo addUserVo(@RequestParam("name") String name) {
		long id = idCount.incrementAndGet();
		UserVo UserVo = new UserVo(id, name);
		userVos.put(id, UserVo);
		return UserVo;
	}

	@ApiOperation(value = "获取用户列表", notes = "获取所有用户的列表")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public List<UserVo> getUserVos() {
		return new ArrayList<UserVo>(userVos.values());
	}

	@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public UserVo getUserVo(@PathVariable Long id) {
		return userVos.get(id);
	}

	@ApiOperation(value = "更新用户名", notes = "根据url的id来指定更新用户名")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"), @ApiImplicitParam(name = "name", value = "用户名称", required = true) })
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public void putUserVo(@PathVariable Long id, @RequestParam("name") String name) {
		userVos.get(id).setName(name);
	}

	@ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public void deleteUserVo(@PathVariable Long id) {
		userVos.remove(id);
	}
}
